home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000006.txt < prev    next >
Text File  |  2013-04-03  |  9KB  |  297 lines

  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5.  
  6. cr.define('bmm', function() {
  7.   /** @const */ var Tree = cr.ui.Tree;
  8.   /** @const */ var TreeItem = cr.ui.TreeItem;
  9.  
  10.   var treeLookup = {};
  11.   var tree;
  12.  
  13.   // Manager for persisting the expanded state.
  14.   var expandedManager = {
  15.     /**
  16.      * A map of the collapsed IDs.
  17.      * @type {Object}
  18.      */
  19.     map: 'bookmarkTreeState' in localStorage ?
  20.         JSON.parse(localStorage['bookmarkTreeState']) : {},
  21.  
  22.     /**
  23.      * Set the collapsed state for an ID.
  24.      * @param {string} The bookmark ID of the tree item that was expanded or
  25.      *     collapsed.
  26.      * @param {boolean} expanded Whether the tree item was expanded.
  27.      */
  28.     set: function(id, expanded) {
  29.       if (expanded)
  30.         delete this.map[id];
  31.       else
  32.         this.map[id] = 1;
  33.  
  34.       this.save();
  35.     },
  36.  
  37.     /**
  38.      * @param {string} id The bookmark ID.
  39.      * @return {boolean} Whether the tree item should be expanded.
  40.      */
  41.     get: function(id) {
  42.       return !(id in this.map);
  43.     },
  44.  
  45.     /**
  46.      * Callback for the expand and collapse events from the tree.
  47.      * @param {!Event} e The collapse or expand event.
  48.      */
  49.     handleEvent: function(e) {
  50.       this.set(e.target.bookmarkId, e.type == 'expand');
  51.     },
  52.  
  53.     /**
  54.      * Cleans up old bookmark IDs.
  55.      */
  56.     cleanUp: function() {
  57.       for (var id in this.map) {
  58.         // If the id is no longer in the treeLookup the bookmark no longer
  59.         // exists.
  60.         if (!(id in treeLookup))
  61.           delete this.map[id];
  62.       }
  63.       this.save();
  64.     },
  65.  
  66.     timer: null,
  67.  
  68.     /**
  69.      * Saves the expanded state to the localStorage.
  70.      */
  71.     save: function() {
  72.       clearTimeout(this.timer);
  73.       var map = this.map;
  74.       // Save in a timeout so that we can coalesce multiple changes.
  75.       this.timer = setTimeout(function() {
  76.         localStorage['bookmarkTreeState'] = JSON.stringify(map);
  77.       }, 100);
  78.     }
  79.   };
  80.  
  81.   // Clean up once per session but wait until things settle down a bit.
  82.   setTimeout(expandedManager.cleanUp.bind(expandedManager), 1e4);
  83.  
  84.   /**
  85.    * Creates a new tree item for a bookmark node.
  86.    * @param {!Object} bookmarkNode The bookmark node.
  87.    * @constructor
  88.    * @extends {TreeItem}
  89.    */
  90.   function BookmarkTreeItem(bookmarkNode) {
  91.     var ti = new TreeItem({
  92.       label: bookmarkNode.title,
  93.       bookmarkNode: bookmarkNode,
  94.       // Bookmark toolbar and Other bookmarks are not draggable.
  95.       draggable: bookmarkNode.parentId != ROOT_ID
  96.     });
  97.     ti.__proto__ = BookmarkTreeItem.prototype;
  98.     treeLookup[bookmarkNode.id] = ti;
  99.     return ti;
  100.   }
  101.  
  102.   BookmarkTreeItem.prototype = {
  103.     __proto__: TreeItem.prototype,
  104.  
  105.     /** @override */
  106.     remove: function(child) {
  107.       TreeItem.prototype.remove.call(this, child);
  108.       if (child.bookmarkNode)
  109.         delete treeLookup[child.bookmarkNode.id];
  110.     },
  111.  
  112.     /**
  113.      * The ID of the bookmark this tree item represents.
  114.      * @type {string}
  115.      */
  116.     get bookmarkId() {
  117.       return this.bookmarkNode.id;
  118.     }
  119.   };
  120.  
  121.   /**
  122.    * Asynchronousy adds a tree item at the correct index based on the bookmark
  123.    * backend.
  124.    *
  125.    * Since the bookmark tree only contains folders the index we get from certain
  126.    * callbacks is not very useful so we therefore have this async call which
  127.    * gets the children of the parent and adds the tree item at the desired
  128.    * index.
  129.    *
  130.    * This also exoands the parent so that newly added children are revealed.
  131.    *
  132.    * @param {!cr.ui.TreeItem} parent The parent tree item.
  133.    * @param {!cr.ui.TreeItem} treeItem The tree item to add.
  134.    * @param {Function=} f A function which gets called after the item has been
  135.    *     added at the right index.
  136.    */
  137.   function addTreeItem(parent, treeItem, opt_f) {
  138.     chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) {
  139.       var index = children.filter(bmm.isFolder).map(function(item) {
  140.         return item.id;
  141.       }).indexOf(treeItem.bookmarkNode.id);
  142.       parent.addAt(treeItem, index);
  143.       parent.expanded = true;
  144.       if (opt_f)
  145.         opt_f();
  146.     });
  147.   }
  148.  
  149.  
  150.   /**
  151.    * Creates a new bookmark list.
  152.    * @param {Object=} opt_propertyBag Optional properties.
  153.    * @constructor
  154.    * @extends {HTMLButtonElement}
  155.    */
  156.   var BookmarkTree = cr.ui.define('tree');
  157.  
  158.   BookmarkTree.prototype = {
  159.     __proto__: Tree.prototype,
  160.  
  161.     decorate: function() {
  162.       Tree.prototype.decorate.call(this);
  163.       this.addEventListener('expand', expandedManager);
  164.       this.addEventListener('collapse', expandedManager);
  165.       bmm.tree = this;
  166.     },
  167.  
  168.     handleBookmarkChanged: function(id, changeInfo) {
  169.       var treeItem = treeLookup[id];
  170.       if (treeItem)
  171.         treeItem.label = treeItem.bookmarkNode.title = changeInfo.title;
  172.     },
  173.  
  174.     handleChildrenReordered: function(id, reorderInfo) {
  175.       var parentItem = treeLookup[id];
  176.       // The tree only contains folders.
  177.       var dirIds = reorderInfo.childIds.filter(function(id) {
  178.         return id in treeLookup;
  179.       }).forEach(function(id, i) {
  180.         parentItem.addAt(treeLookup[id], i);
  181.       });
  182.     },
  183.  
  184.     handleCreated: function(id, bookmarkNode) {
  185.       if (bmm.isFolder(bookmarkNode)) {
  186.         var parentItem = treeLookup[bookmarkNode.parentId];
  187.         var newItem = new BookmarkTreeItem(bookmarkNode);
  188.         addTreeItem(parentItem, newItem);
  189.       }
  190.     },
  191.  
  192.     handleMoved: function(id, moveInfo) {
  193.       var treeItem = treeLookup[id];
  194.       if (treeItem) {
  195.         var oldParentItem = treeLookup[moveInfo.oldParentId];
  196.         oldParentItem.remove(treeItem);
  197.         var newParentItem = treeLookup[moveInfo.parentId];
  198.         // The tree only shows folders so the index is not the index we want. We
  199.         // therefore get the children need to adjust the index.
  200.         addTreeItem(newParentItem, treeItem);
  201.       }
  202.     },
  203.  
  204.     handleRemoved: function(id, removeInfo) {
  205.       var parentItem = treeLookup[removeInfo.parentId];
  206.       var itemToRemove = treeLookup[id];
  207.       if (parentItem && itemToRemove)
  208.         parentItem.remove(itemToRemove);
  209.     },
  210.  
  211.     insertSubtree: function(folder) {
  212.       if (!bmm.isFolder(folder))
  213.         return;
  214.       var children = folder.children;
  215.       this.handleCreated(folder.id, folder);
  216.       for (var i = 0; i < children.length; i++) {
  217.         var child = children[i];
  218.         this.insertSubtree(child);
  219.       }
  220.     },
  221.  
  222.     /**
  223.      * Returns the bookmark node with the given ID. The tree only maintains
  224.      * folder nodes.
  225.      * @param {string} id The ID of the node to find.
  226.      * @return {BookmarkTreeNode} The bookmark tree node or null if not found.
  227.      */
  228.     getBookmarkNodeById: function(id) {
  229.       var treeItem = treeLookup[id];
  230.       if (treeItem)
  231.         return treeItem.bookmarkNode;
  232.       return null;
  233.     },
  234.  
  235.     /**
  236.      * Fetches the bookmark items and builds the tree control.
  237.      */
  238.     reload: function() {
  239.       /**
  240.        * Recursive helper function that adds all the directories to the
  241.        * parentTreeItem.
  242.        * @param {!cr.ui.Tree|!cr.ui.TreeItem} parentTreeItem The parent tree
  243.        *     element to append to.
  244.        * @param {!Array.<BookmarkTreeNode>} bookmarkNodes A list of bookmark
  245.        *     nodes to be added.
  246.        * @return {boolean} Whether any directories where added.
  247.        */
  248.       function buildTreeItems(parentTreeItem, bookmarkNodes) {
  249.         var hasDirectories = false;
  250.         for (var i = 0, bookmarkNode; bookmarkNode = bookmarkNodes[i]; i++) {
  251.           if (bmm.isFolder(bookmarkNode)) {
  252.             hasDirectories = true;
  253.             var item = new BookmarkTreeItem(bookmarkNode);
  254.             parentTreeItem.add(item);
  255.             var anyChildren = buildTreeItems(item, bookmarkNode.children);
  256.             item.expanded = anyChildren && expandedManager.get(bookmarkNode.id);
  257.           }
  258.         }
  259.         return hasDirectories;
  260.       }
  261.  
  262.       var self = this;
  263.       chrome.bookmarkManagerPrivate.getSubtree('', true, function(root) {
  264.         self.clear();
  265.         buildTreeItems(self, root[0].children);
  266.         cr.dispatchSimpleEvent(self, 'load');
  267.       });
  268.     },
  269.  
  270.     /**
  271.      * Clears the tree.
  272.      */
  273.     clear: function() {
  274.       // Remove all fields without recreating the object since other code
  275.       // references it.
  276.       for (var id in treeLookup) {
  277.         delete treeLookup[id];
  278.       }
  279.       this.textContent = '';
  280.     },
  281.  
  282.     /** @override */
  283.     remove: function(child) {
  284.       Tree.prototype.remove.call(this, child);
  285.       if (child.bookmarkNode)
  286.         delete treeLookup[child.bookmarkNode.id];
  287.     }
  288.   };
  289.  
  290.   return {
  291.     BookmarkTree: BookmarkTree,
  292.     BookmarkTreeItem: BookmarkTreeItem,
  293.     treeLookup: treeLookup,
  294.     tree: tree
  295.   };
  296. });
  297.